using System;
using System.Collections;
using System.Collections.Generic;
using MFarm.Dialogue;
using UnityEditor;
using UnityEngine;

public class NPCManager : Singleton<NPCManager>
{
    public SceneRouteDetaList_SO SceneRouteData;

    public List<GameObject> npc;
    
    public List<NPCPosition> npcPositionsList;

    private Dictionary<string, SceneRoute> SceneRouteDict = new Dictionary<string, SceneRoute>();

    protected override void Awake()
    {
        base.Awake();
        InitSceneRouteDict();
    }

    private void OnEnable()
    {
        EventHandler.StartNewGameEvent += OnStartNewGameEvent;
        EventHandler.FirstInFarm += OnFirstInFarm;
        EventHandler.Normal += OnNormal;
    }

    private void OnDisable()
    {
        EventHandler.StartNewGameEvent -= OnStartNewGameEvent;
        EventHandler.FirstInFarm -= OnFirstInFarm;
        EventHandler.Normal += OnNormal;
    }

    

    private void OnStartNewGameEvent(int obj)
    {
        foreach (var charactor in npcPositionsList)
        {
            charactor.npc.position = charactor.position;
            charactor.npc.GetComponent<NPCMovement>().StartScene = charactor.startScene;
        }
    } 
    
    private void OnFirstInFarm()
    {
        NPCMovement mayormove = npc.Find(n => n.name == "NPC_Mayor").GetComponent<NPCMovement>();
        NPCMovement salegirlmove=npc.Find(n => n.name == "NPC_SaleGirl").GetComponent<NPCMovement>();
        salegirlmove.SwitchSchedule(Resources.Load<ScheduleDataList_SO>("GameData/Npcdata/NPC_Data/SaleGirlBegin"));
    }
    
    private void OnNormal()
    {
        //老人
        NPCMovement mayormove = npc.Find(n => n.name == "NPC_Mayor").GetComponent<NPCMovement>();
        DialogueController mayorDialogueController=npc.Find(n => n.name == "NPC_Mayor").GetComponent<DialogueController>();
        //老人AI
        mayormove.SwitchSchedule(Resources.Load<ScheduleDataList_SO>("GameData/Npcdata/NPC_Data/OldManScheduleDataList_SO"));
        //老人对话
        mayorDialogueController.dialogueList =
            Resources.Load<DialoguiList_SO>("GameData/Dialogue_Data/Normal/Mayor");
        mayorDialogueController.FillDialogueStack();
        mayorDialogueController.OnFinishEvent.RemoveAllListeners();
        //女孩
        NPCMovement salegirlmove=npc.Find(n => n.name == "NPC_SaleGirl").GetComponent<NPCMovement>();
        DialogueController salegirlDialogueController=npc.Find(n => n.name == "NPC_SaleGirl").GetComponent<DialogueController>();
        //女孩AI
        salegirlmove.SwitchSchedule(Resources.Load<ScheduleDataList_SO>("GameData/Npcdata/NPC_Data/SaleGirlScheduleDataList_SO"));
        salegirlmove.interactable = false;
        //女孩对话
        salegirlDialogueController.dialogueList=Resources.Load<DialoguiList_SO>("GameData/Dialogue_Data/Normal/SaleGirl");
        salegirlDialogueController.FillDialogueStack();
        salegirlDialogueController.OnFinishEvent.RemoveAllListeners();
        salegirlDialogueController.OnFinishEvent.AddListener(salegirlmove.gameObject.GetComponent<SaleGirlFunction>().OpenShop);
    }

    private void InitSceneRouteDict()
    {
        if (SceneRouteData.sceneRoutesList.Count>0)
        {
            foreach (SceneRoute route in SceneRouteData.sceneRoutesList)
            {
                var key = route.fromSceneName + route.gotoSceneName;
                if (SceneRouteDict.ContainsKey(key))
                    continue;
                else
                    SceneRouteDict.Add(key,route);
            }
        }
    }


    public SceneRoute GetSceneRoute(string fromSceneName, string gotoSceneName)
    {
        return SceneRouteDict[fromSceneName + gotoSceneName];
    }
}
